/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: message_receiver.h
 *
 * Purpose: a message receiver define and implementation
 *
 * Developer:
 *   wen.gu , 2023-08-01
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_TRANSPORT_MESSAGE_RECEIVER_H__
#define __XBUS_TRANSPORT_MESSAGE_RECEIVER_H__

#include "xbus/core/xresult.h"
#include "xbus/core/xmessage.h"

#include "xbus/transport/message_worker.h"
#include "xbus/transport/xmessage_queue.h"

namespace xbus::transport {

class MessageReceiver: public MessageWorker<XMessageQueue> {
public:
    MessageReceiver(const std::string& name, int32_t priority) : MessageWorker<XMessageQueue>(name, priority) { }
    virtual ~MessageReceiver() = default;

public: /** receiver API */
    /** timeout_ms: see also 'class Timeout',
     * NoWait: if haven't message received, then return immediately
     * WaitForever: if haven't message received, then recv() will be block until receive a message or the XEndpoint is stopped
     * > 0: if haven't message received, then recv() will be block until receive a message or the XEndpoint is stopped or timeout
    */
    virtual core::XResult<core::XMessagePtr> recv(int32_t timeout_ms = core::WaitType::WaitForever) {
        return (msg_queue_ != nullptr) ? msg_queue_->pop(timeout_ms) : core::XResult<core::XMessagePtr>::FromError(XErrC::InvalidStatus);
    } 
};


} //namespace xbus::transport


#endif /** !__XBUS_TRANSPORT_MESSAGE_RECEIVER_H__ */